package com.etp.actions;

import com.etp.dao.beans.LoginBean;
import com.etp.dao.impl.LoginImpl;
import com.etp.dao.interfaces.ILogin;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	
	private static final long serialVersionUID = 5450894297309814752L;
	
	private String userName;
	private String password;

	public String getUserName() {
		return userName.toUpperCase();
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		LoginBean loginBean = new LoginBean();
		loginBean.setUserName(getUserName());
		loginBean.setPswd(getPassword());
		ILogin mLogin = new LoginImpl();
		
		if(mLogin.validateLogin(loginBean)){
			return ActionSupport.SUCCESS;
		}
		else {
			return ActionSupport.ERROR;
		}
	}

	public void validate() {
		
		System.out.println("validate Called");
		if (getUserName().length() == 0) {

			addFieldError("userName", getText("username.required"));

		} 
		if (getPassword().length() == 0) {

			addFieldError("password", getText("password.required"));

		}

	}
}
